﻿<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewMediaUserControl.ascx.cs"
    Inherits="JM.JCY.SharePointComponent.PhotoAlbum.ViewMedia.ViewMediaUserControl" %>
<style type="text/css">
    #Textme
    {
        width: 250px;
    }
    
    .handMoveOnCluser
    {
        cursor: hand;
    }
    .lkbNextHidden
    {
        display: none;
    }
</style>
<script type="text/javascript">

    var belongto = null;
    var currentgroup = null;
    var enable = null;
    var albumurl = null;
    var cancelimage="/_layouts/JM.JCY.Resource/images/cancel.png";
    var saveImage="/_layouts/JM.JCY.Resource/images/save.png";

    var imagesArrow = [];
    var currentid;
    var playsetting;
    var imageArrows;
    var tempText;  
    var isDisable;
    var userIDnames=[];
    var idnames;
   
    var wcfUrl = PhotoAlbumUrl;
    var wcfnewsfeedUrl=newsfeedSvcUrl;
    var editCaption=false;
    var checkEditCatptionNum=0;
    var fristIsMedia=false;

    var MediaURLField = "";
    var PreviewURLField = "";
    $(document).keydown(function(){ 
        if(event.keyCode == 13)
         {
            var cancel=$("#divCancel").html();
            if(cancel.indexOf(cancelimage)>0)
            {
                addclick(true);
            }
          return false;
         }
     }); 
    $(document).ready(function () {

         // redirect if no context
        var picName = $("#<%=HiddenPicName.ClientID %>").val();
        var albumDesc = $("#<%=HiddenDesc.ClientID %>").val();
        var albumId = $("#<%=HiddenAlbumId.ClientID %>").val();

        isDisable=$("#<%=HiddenDisable.ClientID %>").val();
    
        LoadDescTxt(albumDesc);
       
        var LBacktoAlbums = $("#<%=lbBack2Album.ClientID %>");
        var LBacktoAlbums1 = $("#<%=lbBack2Album1.ClientID %>");
        LBacktoAlbums.live('click', function () {
            location.href = "ViewAlbum.aspx?AlbumId=" + albumId;
            return false;
        });

        LBacktoAlbums1.live('click', function () {
            location.href = "ViewAlbum.aspx?AlbumId=" + albumId;
            return false;
        });

        var removeThisMedia=$("#aRemoveThisMedia");
        removeThisMedia.live('click',function(){
        var mediaid=$("#<%=HiddenUpdateMediaId.ClientID %>").val();
                if(imagesArrow.length==1)
                {
                    showConfirm('',"这张是这个删除的最后一张照片，删除它会删除整个相册，还要删除吗？",RemoveMediaDoCall,CancelCall,null);
                }else
                {
                    showConfirm('',"你真的要删除这张照片吗？",RemoveMediaDoCall,CancelCall,null);
                 }
            return false;
        });

        $("#divOp").click(function (){

        });

        $("#alike").click(function(){
            doLike();
            return false;
        });
        // play pic
        $("#imgnext").click(function () {
            nextPic();
        });

        $("#imgback").click(function () {
            backPic();
        });

        $("#imgfirst").click(function () {
            firstPic();
        });

        $("#imglast").click(function () {
            lastPic();
        });

        $("#imgnextDown").click(function () {
            nextPic();
        });

        $("#imgbackDown").click(function () {
            backPic();
        });

        $("#imgfirstDown").click(function () {
            firstPic();
        });

        $("#imglastDown").click(function () {
            lastPic();
        });

        imageArrows = <asp:Literal ID="llArrows" runat="server"></asp:Literal>;
        idnames=<asp:Literal ID="lidnames" runat="server"></asp:Literal>;
        var play = new playPic({
            playid: "imgpic",
            imageArrows: imageArrows,
            rotateplay: true,
            picname:picName,
            idnames:idnames
        });

        if(isDisable=='true')
        {
        $("#tdControlsUp").hide();
        $("#tdControlsDown").hide();
        $("#tdControlsVisible").show();

        }

        checkCanBeEdited();
        displayComments();
    });

    function RemoveMediaDoCall()
    {
      __doPostBack('AlbumRemoveThisMedia','');
    }

    function CancelCall()
    {
    //Do nothing.
    }

    function checkEditCaption()
    {
        var cancelText=$("#divCancel").html();//Cancel
        if(editCaption==true && checkEditCatptionNum<10)
        {
            checkEditCatptionNum=checkEditCatptionNum+1;
            if(cancelText.indexOf(cancelimage)<0)
           {
            addclick(false);
            }
            setTimeout(function(){checkEditCaption();},500);
        }
    }

    function displayComments()
    {
        OCP.Newsfeed.SPObjNewsFeed.displayComments(getSharepointObjectId()); 
    }

     // load feed post method
    function LoadDescTxt(_albumDesc)
    {
      if (_albumDesc == '') {
            $("#divText").text('');
            $("#divOp").text("添加标题");
        } else {
            $("#divText").text(_albumDesc);
            $("#divOp").text("编辑标题");
            
        }
        $("#divCancel").text("");
    }

    function firstPic() {
    currentid=1;
    loadPicSetting(currentid);
    displayComments();
    }

    function lastPic() {
        currentid = imagesArrow.length;
        loadPicSetting(currentid);
         displayComments();
    }

     function nextPic() {
            if (currentid == imagesArrow.length) {
                   if (playsetting.rotateplay) {
                   currentid = 1;
                   loadPicSetting(currentid);
                }
                    else {
                    // if not rotateplay. do sth.
                        return;
                    }
            } else
            {
                currentid = currentid + 1;
                loadPicSetting(currentid);
             }
    displayComments();
    }

   function backPic() {
     if (currentid == 1) {
            if (playsetting.rotateplay) {
                currentid = imagesArrow.length;
            loadPicSetting(currentid);
            }
            else {
            // if rotateplay, do sth.
                return;
            }
      } else {
            currentid = currentid - 1;
            loadPicSetting(currentid);          
     }
        displayComments();
    }

    function loadPicSetting(currentId)
    {
        var divImg=$("#divImg");
        divImg.show();
        $("#imgpic").attr({ src: imagesArrow[currentId - 1].src });
        $("#imgpic").attr({ alt: imagesArrow[currentId - 1].title });

        $("#<%=HiddenImgOriginalUrl.ClientID %>").val(imagesArrow[currentId - 1].longdesc);
        $("#<%=HiddenImgFullName.ClientID %>").val(imagesArrow[currentId - 1].alt);
        updateDescTxt(imagesArrow[currentId - 1].hspace);
        $("#<%=HiddenUpdateMediaId.ClientID %>").val(imagesArrow[currentId - 1].hspace);
        showCommonMsg(currentId);        
    }

    function showCommonMsg(currentId)
    {
         var albumName=$("#<%=HiddenFieldAlumName.ClientID %>").val();
         $("#albumNameMediaNum").text(albumName + " - 照片 " + currentId + " / " + imagesArrow.length);
         $("#userIdName").html("照片上传人： <a href='/PersonalSite/PersonalPages/PublicUser.aspx?StaffId="+userIDnames[currentId - 1].hspace +"'>" + userIDnames[currentId - 1].title + "</a>");
        $("#<%=HiddenCurrentId.ClientID %>").val(currentId);
    }

    function playPic(settings) {

        imagesArrow = [];
        for( var j=0;j<settings.idnames.length;j++)
        {
            userIDnames[j]=new Image();
            userIDnames[j].hspace=settings.idnames[j][0];//staffid
            userIDnames[j].title=settings.idnames[j][1];//user name
            userIDnames[j].src=settings.idnames[j][2];//ThumbNailImgUrl
        }

        for (var i = 0; i < settings.imageArrows.length; i++) {
            imagesArrow[i] = new Image();
            imagesArrow[i].src = settings.imageArrows[i][0];
            imagesArrow[i].title = settings.imageArrows[i][1];
            imagesArrow[i].longdesc = settings.imageArrows[i][2];
            imagesArrow[i].alt = settings.imageArrows[i][3];
            imagesArrow[i].hspace=settings.imageArrows[i][4];
            imagesArrow[i].vspace=settings.imageArrows[i][5];
        
        if(settings.imageArrows[i][0].toString().indexOf(settings.picname)>-1)
                {
                  currentid=i+1;
                }
         }
        playsetting = settings;
       loadPicSetting(currentid);   
    }

      function addclick(isClick) {
        if(isClick==true)
        {
            checkEditCatptionNum=1000;
        }
        var text = $("#divText").text();
        var opName = $("#divOp").html();
        if (opName == "添加标题") {

            $("#divCancel").html("<img src='"+cancelimage+"'/>");
            $("#divOp").html("<img src='"+saveImage+"'/>");
            $("#divText").html("<input id='Textme' type='text'/>");
            $("#Textme").focus();
        }
        if (opName == "编辑标题") {

            $("#divCancel").html("<img src='"+cancelimage+"'/>");
            $("#divOp").html("<img src='"+saveImage+"'/>");
            $("#divText").html("<input id='Textme' type='text'/>");
            $("#Textme").val(text);
            $("#Textme").focus(function(){$("#Textme").val($("#Textme").val());});
            $("#Textme").focus();
        }

        if (opName.indexOf(saveImage)>0){
            text = $("#Textme").val();
            $("#Textme").remove();
          
          // update desc textbox
           var updateMediaId= $("#<%=HiddenUpdateMediaId.ClientID %>").val();       
           saveDesc(text,updateMediaId);

            if (text == "") {
                $("#divOp").text("添加标题");
            } else {
                $("#divOp").text("编辑标题");
            }
            $("#divCancel").text("");
        }
          tempText=text;
    }

    function cancelclick() {
         checkEditCatptionNum=1000;
         if (tempText == "") {
                $("#divOp").text("添加标题");
                $("#divText").text("");
            } else {
           
                $("#divOp").text("编辑标题");
                $("#divText").text(tempText);
            }

            $("#divCancel").text("");
    }

   function copyclick()
    {
      clipboardData.setData('text',window.location.href) 
    }

    function saveDesc(_text,_mediaId)
    {
         var albumId = $("#<%=HiddenAlbumId.ClientID %>").val();         
         var userName = $("#<%=HiddenUserName.ClientID %>").val();
         var staffId= $("#<%=HiddenStaffId.ClientID %>").val();

            // update data table
             $.ajax({
                type: "Get",
                url: wcfUrl+"UpdateMedia?desc="+_text+"&albumId="+albumId+"&mediaId="+_mediaId+"&userName="+userName+"&staffId="+staffId,
                contentType: "application/json; charset=utf-8",
                dataType: 'jsonp',
                success: function (result) {
                   $("#divText").text(_text);
                   imagesArrow[currentid - 1].title=_text;
                   $("#imgpic").attr({ alt: imagesArrow[currentid - 1].title });
                },
                error:function(a,b,c){alert(c);},
                failure: function (msg) {
                    alert(msg);
                }
            });
            // end
    }

    function updateDescTxt(_mediaId)
    {
             $.ajax({
                type: "Get",              
                url: wcfUrl+"GetMediaDescriptionById?mediaId="+_mediaId,
                contentType: "application/json; charset=utf-8",
                dataType: 'jsonp',
                success: function (result) {
                var JSON = eval('(' + (result) + ')');
                LoadDescTxt(JSON[0].MediaDescription);
                $("#timeagoSpan").text($.timeago(JSON[0].LastUpdatedTime));
                  getLikes(JSON[0].objectId);
                },
                error:function(a,b,c){alert(c);},
                failure: function (msg) {
                    alert(msg);
                }
            });
    }

    function getLikes(objectId)
    {
        var objectType=getObjectType();

        $.ajax({
                type: "Get",
                url: wcfUrl+"GetLikes?staffId="+getStaffId()+"&objectId="+objectId+"&objectType="+objectType,
                contentType: "application/json; charset=utf-8",
                dataType: 'jsonp',
                success: function (result) {
                if (result == "[]") {
                            showLikeNum(0);
                            showLikeTitle("喜欢");
                        } else {
                            var JSON = eval('(' + (result) + ')');

                            showLikeNum(JSON[0].count);
                            if(JSON[0].staffId==getStaffId())
                            {
                                showLikeTitle("一般");
                            }else
                            {
                                showLikeTitle("喜欢");
                            }
                        }
                },
                error:function(a,b,c){alert(c);},
                failure: function (msg) {
                    alert(msg);
                }
            });
    }

    function showLikeNum(likenum)
    {
        $("#likenum").text(likenum);
    }
    function showLikeTitle(likeTitle)
    {
        $("#alike").text(likeTitle);
    }
    function getStaffId()
    {
    var staffId=$("#<%=HiddenStaffId.ClientID %>").val();
    return staffId;

    }

    function getSharepointObjectId()
    { 
    var _currentId= $("#<%=HiddenCurrentId.ClientID %>").val();   
    var _sharepoinObjectId= imagesArrow[_currentId - 1].vspace;
    return _sharepoinObjectId;
   
    }

   function checkCanBeEdited() {
        enable = $("#<%=HiddenFieldEableEdit.ClientID %>").val();
        albumurl = $("#<%=HiddenFieldAlbumUrl.ClientID %>").val();
        if (enable == "T") {
            $("#divOp").css("display", "block");
            $("#divEdit").css("display", "block");
            $("#divCom").css("display", "none");
        } else {
            $("#divOp").css("display", "none");
            $("#divEdit").css("display", "none");
            $("#divCom").css("display", "block");
        }
    }

 function redirectComment()
  {
  var txtComment=$("#txtComment");
  txtComment.focus();
  }

  function doLike()
  {
    var objectId=getSharepointObjectId();
    var staffId= $("#<%=HiddenStaffId.ClientID %>").val();
    var objectType=getObjectType();
      $.ajax({
                type: "Get",
                url: wcfUrl+"LikeOfAlbum?staffId="+staffId+"&objectId="+objectId+"&objectType="+objectType,
                contentType: "application/json; charset=utf-8",
                dataType: 'jsonp',
                success: function (result) {
                getLikes(objectId);
                },
                error:function(a,b,c){alert(c);},
                failure: function (msg) {
                    alert(msg);
                }
            });

      return false;
  }

  function getObjectType()
  {
   return "pic";
  }

</script>
<div class="gallery">
    <table width="100%" class="galleryTitle">
        <tr>
            <td>
                <h2 class="gallery">
                    <img src="/_layouts/JM.JCY.Resource/images/相册小.png" width="24">
                    我的相册 - 查看照片</h2>
            </td>
            <td align="right" class="link">
                <div id="divEdit">
                  <img src="/_layouts/JM.JCY.Resource/images/delete.png" width="12"/>
                    <a class="galleryDetail" id="aRemoveThisMedia" href="#">删除照片</a></div>
                <div id="divCom">
                  </div>
            </td>
        </tr>
        <tr><td colspan=2><br /><hr /></td></tr>
        <tr>
            <td colspan="2">
                <h2 id="albumNameMediaNum" class="galleryDetail">
                </h2>
            </td>
        </tr>
        <tr>
            <td>
                <h2 id="userIdName" class="galleryDetail">
                </h2>
            </td>
            <td align="right" id="tdControlsVisible" style="display: none">
                <img src="/_layouts/JM.JCY.Resource/images/back_link.png" width="12"/>
                <asp:LinkButton ID="lbBack2Album1" runat="server" CssClass="galleryDetail">返回相册</asp:LinkButton>
            </td>
            <td align="right" id="tdControlsUp">
                              <img src="/_layouts/JM.JCY.Resource/images/back_link.png" width="12"/>
                <asp:LinkButton ID="lbBack2Album" runat="server" CssClass="galleryDetail">返回相册</asp:LinkButton>
                &nbsp;&nbsp;<span class="pagination"><a id="imgfirst" class="handMoveOnCluser">&lt;&lt;
                    第一张</a>&nbsp; | <a id="imgback" class="handMoveOnCluser">&lt; 上一张</a>&nbsp;
                    | <a id="imgnext" class="handMoveOnCluser">下一张 &gt;</a>&nbsp; | <a id="imglast"
                        class="handMoveOnCluser">最后一张 &gt;&gt;</a></span>
            </td>
        </tr>
    </table>
    <br />
    <table class="photo" width="100%">
        <tr>
            <td colspan="2" align="center">
                <div id="divImg">
                    <img id="imgpic" alt="" />
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left" class="albumdesc">
                <table>
                    <tr>
                        <td>
                            <div id="divText">
                            </div>
                        </td>
                        <td>
                            <a id="divOp" class="galleryDetail" href="javascript:addclick(true)">添加标题</a>
                        </td>
                        <td>
                            <a id="divCancel" class="galleryDetail" href="javascript:cancelclick()">取消</a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <h2 class="galleryDetail">
                    <span id="timeagoSpan"></span>&nbsp;|
                    <img src="/_layouts/JM.JCY.Resource/images/thumbs-up.gif">&nbsp;<span id="likenum">0</span>
                    &nbsp;&nbsp;<a class="galleryDetail" id="alike" href="#">喜欢</a> | <a class="galleryDetail"
                        href="javascript:redirectComment();">评论</a> |
                    <asp:LinkButton ID="lbDownload" runat="server" Text="下载" CssClass="galleryDetail"
                        OnClick="lbDownload_Click"></asp:LinkButton></h2>
            </td>
            <td align="right" id="tdControlsDown">
                <span class="pagination"><a id="imgfirstDown" class="handMoveOnCluser">&lt;&lt; 第一张</a>&nbsp;
                    | <a id="imgbackDown" class="handMoveOnCluser">&lt; 上一张</a>&nbsp; | <a id="imgnextDown"
                        class="handMoveOnCluser">下一张 &gt;</a>&nbsp; | <a id="imglastDown" class="handMoveOnCluser">
                            最后一张 &gt;&gt;</a></span>
            </td>
        </tr>
    </table>
    <asp:HiddenField ID="HiddenMediaId" runat="server" />
    <asp:HiddenField ID="HiddenAlbumId" runat="server" />
    <asp:HiddenField ID="HiddenPicName" runat="server" />
    <asp:HiddenField ID="HiddenDesc" runat="server" />
    <asp:HiddenField ID="HiddenUserName" runat="server" />
    <asp:HiddenField ID="HiddenStaffId" runat="server" />
    <asp:HiddenField ID="HiddenImgOriginalUrl" runat="server" />
    <asp:HiddenField ID="HiddenImgFullName" runat="server" />
    <asp:HiddenField ID="HiddenUpdateMediaId" runat="server" />
    <asp:HiddenField ID="HiddenCurrentId" runat="server" Value="1" />
    <asp:HiddenField ID="HiddenDisable" runat="server" />
    <asp:HiddenField ID="HiddenAppId" runat="server" />
    <asp:HiddenField ID="HiddenFieldEableEdit" runat="server" />
    <asp:HiddenField ID="HiddenFieldAlbumUrl" runat="server" />
    <asp:HiddenField ID="HiddenFieldAlumName" runat="server" />
</div>
